<template>
  <div>

    <template>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="岗位编码">
          <el-input v-model="formInline.postCode" placeholder="请输入岗位编码"></el-input>
        </el-form-item>
        <el-form-item label="岗位名称">
          <el-input v-model="formInline.postName" placeholder="请输入岗位名称"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.status" placeholder="岗位状态">
            <el-option label="正常" value="0"></el-option>
            <el-option label="异常" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="list()">查询</el-button>
          <el-button type="primary"  @click="listAll()">重置</el-button>


        </el-form-item>
      </el-form>
<!--      <el-button @click="updateopen(scope.row)" type="text" size="small">详情</el-button>-->

<!--      <el-button @click="deletebyid(scope.row.postId)" type="text" size="small">删除</el-button>-->
      <el-button  @click="openadd()" type="primary" >新增</el-button>
      <el-button type="success" disabled>修改</el-button>
      <el-button type="warning" disabled>删除</el-button>
      <el-button @click="excel()"  type="danger" >导出</el-button>
      <el-button @click="download()"  type="danger" >导入</el-button>
      <el-button type="warning" plain @click="getTjt">统计图</el-button>
      <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="postCode"
            label="岗位编码"
            width="150">
        </el-table-column>
        <el-table-column
            prop="postName"
            label="岗位名称"
            width="120">
        </el-table-column>
        <el-table-column
            prop="postSort"
            label="岗位排序"
            width="120">
        </el-table-column>


        <el-table-column
            prop="createTime"
            label="创建时间"
            width="120">
        </el-table-column>

        <el-table-column
            prop="status"
            label="状态"
            width="120">
          <template slot-scope="a">
            <el-button v-if="a.row.status==0" type="text" size="small">正常</el-button>
            <el-button v-if="a.row.status!=0" type="text" size="small">异常</el-button>

          </template>
        </el-table-column>

        <el-table-column
            fixed="right"
            label="操作"
            width="100">
          <template slot-scope="scope">
            <el-button @click="updateopen(scope.row)" type="text" size="small">修改</el-button>
            <el-button @click="deletebyid(scope.row)" type="text" size="small">删除</el-button>

          </template>
        </el-table-column>
      </el-table>

    </template>
    <el-dialog title="用户部门统计图" :visible.sync="dialogShowFormVisible">
      <div id="main" style="width: 500px;height: 500px"></div>
    </el-dialog>
    <el-dialog title="添加" :visible.sync="dialogFormVisible" >
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="岗位编码" :label-width="formLabelWidth" prop="postCode">
          <el-input v-model="form.postCode" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="岗位名称" :label-width="formLabelWidth" prop="postName">
          <el-input v-model="form.postName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="岗位顺序" :label-width="formLabelWidth" prop="postSort">
          <el-input-number v-model="form.postSort"  :min="1" :max="10" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="岗位状态" :label-width="formLabelWidth" prop="status">
          <el-radio v-model="form.status" label="0">正常</el-radio>
          <el-radio v-model="form.status" label="1">异常</el-radio>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add('ruleForm')">添加</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改" :visible.sync="dialogFormVisibletwo">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="岗位编码" :label-width="formLabelWidth" prop="postCode">
          <el-input v-model="form.postCode" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="岗位名称" :label-width="formLabelWidth" prop="postName">
          <el-input v-model="form.postName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="岗位顺序" :label-width="formLabelWidth" prop="postSort">
          <el-input-number v-model="form.postSort"  :min="1" :max="10" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="岗位状态" :label-width="formLabelWidth" prop="status">
          <el-radio v-model="form.status" label="0">正常</el-radio>
          <el-radio v-model="form.status" label="1">异常</el-radio>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="update('ruleForm')">修改</el-button>
      </div>
    </el-dialog>
    <el-pagination
        @current-change="handleCurrentChange"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>
  </div>
</template>

<script>
import * as echarts from "echarts";

export default {
  name: "PostView",
  data() {
    return {
      pageNum: 1,
      total: 0,
      pageSize: 3,
      dialogFormVisibletwo: false,
      formInline: {},
      formLabelWidth: "120px",
      form: {},
      tableData: [],
      dialogFormVisible: false,
      multipleSelection: [],
      dialogShowFormVisible:false,
      rules: {
        postCode: [
          {required: true, message: '请输入岗位编码', trigger: 'blur'},
        ],
        postName: [
          {required: true, message: '请输入岗位名称', trigger: 'blur'},
        ],
        postSort: [
          {required: true, message: '请输入岗位顺序', trigger: 'blur'},
        ],
        status: [
          {required: true, message: '请输入状态', trigger: 'blur'},
        ],
      }
    }
  }, methods: {
    getTjt() {
      this.dialogShowFormVisible = true;
      this.$nextTick(() => {
        this.zzt();
      })
    },
    //统计图
    zzt() {
      // let formData = []
      let value1 = []
      let value2 = []

      const chartDom = document.getElementById('main');
      const myChart = echarts.init(chartDom);

      this.axios.get("system/post/postlistechart").then(res => {
        // formData = res.data;
        console.log(res.data)
        value1 = res.data.map(item => item.xdata);
        value2 = res.data.map(item => item.ydata);
        console.log(value1)
        console.log(value2)

        const option = {
          xAxis: {
            type: 'category',
            data: value1  // 直接将获取到的角色名称数组赋值给 xAxis 的 data
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              data: value2,  // 将获取到的数量数组赋值给 series 的 data
              type: 'bar'
            }
          ]
        };
        option && myChart.setOption(option);
      });

      /*
      this.axios.post("system/user/barChart").then(res=>{
        console.log(res);
        this.barChart=res.data.data;
        var y=[];
        var x=[];

        for (let i = 0; i < this.barChart.length; i++) {
          var ydata=this.barChart[i].ydata;
          y.push(ydata)
          var xdata=this.barChart[i].xdata;
          x.push(xdata)
        }
        var chartDom = document.getElementById('main');
        var myChart = echarts.init(chartDom);
        var option;
        option = {
          xAxis: {type: 'category', data: x},
          yAxis: {type: 'value'}, series: [{
              data: y,
              type: 'line'
            }]};
        option && myChart.setOption(option);
      })
   */
    },

  listAll(){
    this.axios.post("/system/post/postlistall?pageNum="+this.pageNum).then(res=>{
      this.tableData=res.data.list;
      this.total=res.data.total;
    })
  },
    handleCurrentChange(page) {
      this.pageNum = page;
      this.listAll(this.pageNum);
    },
    handleSelectionChange(val) {

      this.multipleSelection = val;
      console.log(val)

    },
    excel(){
      location="http://localhost:9017/post/excel";

    },
    download(){
      location="    http://localhost:50137/api/v1/buckets/my-test01/objects/download?prefix=5bKX5L2N5L+h5oGvLnhsc3g=&version_id=null\n";

    },
    deletebyid(row){
    this.form=row;
      this.axios.post("system/post/delete",this.form).then(res=>{
        if (res.status==200){
          this.$message.success("删除成功");

          this.listAll();
        }else{
          this.$message.error("删除失败");
        }
      })
    },
    update(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post("system/post/update",this.form).then(res=>{
            if (res.status==200){
              this.$message.success("修改成功");
              this.dialogFormVisibletwo=false;
              this.list();
            }else{
              this.$message.error("修改失败");
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });


    },
    updateopen(row){
      console.log(row.postId)
      this.dialogFormVisibletwo=true;
      this.form=row;

    },
    add(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post("system/post/add",this.form).then(res=>{

            if (res.status==200){
              this.$message.success("添加成功");
              this.dialogFormVisible=false;
              this.list();
            }else{
              this.$message.error("添加失败");
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });


    },
    openadd(){
      this.dialogFormVisible=true;
      this.form={};

    },

    list(){
      this.axios.post("/system/post/postlist",this.formInline).then(res=>{
        this.tableData=res.data;
      })
    }
  }, created() {
   this.listAll();
  }
}
</script>

<style scoped>

</style>